package com.trkj.entity;

import com.trkj.ann.MyJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@MyJDBC(driver="com.mysql.jdbc.Driver", url="jdbc:mysql://127.0.0.1:3306/tame?useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=GMT%2B8", username="root", password="1234")
public class BaseDao {
    private String driver;
    private String url;
    private String username;
    private String password;

    public BaseDao() {
        MyJDBC annotation = this.getClass().getAnnotation(MyJDBC.class);
        if (annotation != null) {
            driver = annotation.driver();
            url = annotation.url();
            username = annotation.username();
            password = annotation.password();
        }
    }

    public Connection getConnection() {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
